<?php
/**
 * levelPeer.
 *
 * @package    elearning
 * @author     Dvquan
 * @version		1.00.00
 * @Created		xx.08.2010
 * @Update		10.08.2010 => Change doCount method to doCountAdmin
 */
class LevelPeer extends BaseLevelPeer
{
	static public $choices = array(
			null => 'All',
			1 => 'Active',
			0 => 'not Active',
			);

	static public function getLevelStatusChoices()
	{
		return self::$choices;
	}
	//tam comment de chay thang frontend
 /*
	public static function doCount(Criteria $criteria, $distinct = false, PropelPDO $con = null)
	{
		$oldcriteria =  $criteria;
		$criteria->addJoin(LevelI18nPeer::ID, LevelPeer::ID, Criteria::JOIN);
		$currentCulture = sfContext::getInstance()->getUser()->getCulture();
		$criteria->addAnd(LevelI18nPeer::CULTURE, $currentCulture);
		//phan nay chay thi khi tham chieu front end bi loi
		// do bang i18n lai join voi chinh no: ????
		return LevelI18nPeer::doCount($criteria, $distinct, $con);
		try{
		return LevelI18nPeer::doCount($criteria, $distinct, $con);
		}
		catch( Exception $ex)
		{
			return LevelPeer::getCountListLevelWithCourse( 1 );
		}
		//return LevelPeer::doCount($criteria, $distinct, $con);
	}
	*/
	//doCountAdmin
	//Chi dinh peer_count_method trong generator phan admin de change lai ham doCount
	// Giai quyet duoc viec frontend dung ham doCount de phan trang.
	public static function doCountAdmin(Criteria $criteria, $distinct = false, PropelPDO $con = null)
	{
		$oldcriteria =  $criteria;
		$criteria->addJoin(LevelI18nPeer::ID, LevelPeer::ID, Criteria::JOIN);
		$currentCulture = sfContext::getInstance()->getUser()->getCulture();
		$criteria->addAnd(LevelI18nPeer::CULTURE, $currentCulture);
		//phan nay chay thi khi tham chieu front end bi loi
		// do bang i18n lai join voi chinh no: ????
		try{
		return LevelI18nPeer::doCount($criteria, $distinct, $con);
		}
		catch( Exception $ex)
		{
			//return LevelPeer::getCountListLevelWithCourse( 1 );
		}
		//return LevelPeer::doCount($criteria, $distinct, $con);
	}
	//chang hieu tai sao khi add cai lia vao thi no filter theo
	//va add them peer_method o list file generator.yml
	//Khi them phuong thuc nay thi filter thong thuong khong hoat dong.
	public static function doSelectWithI18n(Criteria $c, $culture = null, PropelPDO $con = null)
	{
		return BaseLevelPeer::doSelectWithI18n($c, $culture, $con);
	}
	//Dung o front end get item with i18n
	
	/*
	public static function doSelect(Criteria $criteria, PropelPDO $con = null)
	{
		//$criteria->addJoin(LevelI18nPeer::ID, LevelPeer::ID, Criteria::JOIN);
		//$currentCulture = sfContext::getInstance()->getUser()->getCulture();
		//$criteria->addAnd(LevelI18nPeer::CULTURE, $currentCulture);
		return LevelPeer::doSelect($criteria, $con);
	}
	*/
	//getScourse name
	public static function getCourseNamebyCourseId( $course_id, $lang_current )
	{
		//tra ve mot doi tuong courseI18n
		$record = DbFinder::from('CourseI18n')
				->where('id',$course_id)
				->where('culture',$lang_current)
				->findOne()
				;
		if( isset($record) )
			return $record->getCourseName();
		//$lang_current
	}
	
	//Get du lieu cho trang Level/index
	public static function getListLevelWithCourse( $course_id, $lang_current, $start=1, $limit = 10 )
	{
		//default select all
		if ( $course_id == 0 )
			return DbFinder::from('Level a')
			->with('I18n')
			->where('a.levelStatus',1)
			->orderBy('a.id', 'Asc')
			//->useCache(sfConfig::get('sf_cache'), 200)
			->paginate($start, $limit);
		else //select with course
		{
			return DbFinder::from('Level a')
			->with('I18n')
			->where('a.levelStatus',1)
			->where('a.courseId', $course_id )
			->orderBy('a.id', 'Asc')
			//->useCache(sfConfig::get('sf_cache'), 200)
			->paginate($start, $limit);
		}
	/*
		return DbFinder::from('Level a')
				->with('I18n')
				->leftJoin('Course')
				->leftJoin('CourseI18n b ON Course.id = b.id')
				->where('a.courseId', $course_id )
				->where('a.levelStatus',1)
				->where('b.culture',$lang_current)
				->orderBy('a.id', 'Asc')
				//->useCache(sfConfig::get('sf_cache'), 200)
				->paginate($start, $limit);
	*/
		/*
			with('Category', 'I18n')
			->with('I18n')
			->leftJoin('Course b ON a.course_id = b.id')
			->select(array('Level.ID','level.COURSE_ID','level.LEVEL_PRICE','level.LEVEL_STATUS','level.CREATE_AT', 'level.UPDATE_AT','level_i18n.CULTURE','level_i18n.LEVEL_NAME'))
		*/
	}
	
	//Retrieve by primary key
	public static function getListLevelByprimary( $level_id)
	{
	  return DbFinder::from('Level a')
			->with('I18n')
			->findPk($level_id);
	}
	//
	public static function getCountListLevelWithCourse(  $course_id )
	{
		/*
		$record = DbFinder::from('Level')
				->with('I18n')
				->where('courseId', $course_id )
				->where('levelStatus',1)
				->orderBy('id', 'Asc')
				//->useCache(sfConfig::get('sf_cache'), 200)
				->paginate($start, $limit);
		*/
		return 11;
	}
	//get classes have levelID
	public static function getClassesWithLevel( $level_id )
	{
		return DbFinder::from('Classes a')
				->with('I18n')
				->where('a.clsStatus',1)
				->where('a.levelId', $level_id  )
				->orderBy('a.clsStartTime', 'desc')
				->find()
				;	
	}

}
